<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增服务' : '编辑服务'" append-to-body width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="服务名称" prop="name">
        <el-input v-model="form.name" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="服务路径" prop="path">
        <el-input v-model="form.path" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="排序号" prop="sort">
        <el-input-number v-model.number="form.sort" :min="1" controls-position="right" style="width: 370px;" />
      </el-form-item>

      <el-form-item label="状态" prop="isMenu">
        <el-radio v-for="item in dicts" :key="item.value" v-model="form.isMenu" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item>
      <el-form-item v-if="form.isMenu == '0'" label="上级服务">
        <el-select v-model="form.pid" style="width: 370px" placeholder="请选择上级服务">
          <el-option
            v-for="item in services"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.id === form.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/auth/service'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    dicts: {
      type: Array,
      required: true
    },
    services: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: { name: '', path: '', sort: 1, isMenu: '0', pid: null },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '请输入路径', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      if (this.form.isMenu === '0' && !this.form.pid) {
        this.$message({
          message: '上级服务不能为空',
          type: 'warning'
        })
      } else {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.isAdd) {
              this.doAdd()
            } else this.doEdit()
          } else {
            return false
          }
        })
      }
    },
    doAdd() {
      if (this.form.isMenu === '1') {
        this.form.pid = 0
      }
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      if (this.form.isMenu === '1') {
        this.form.pid = 0
      }
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'] && this.$refs['form'].resetFields()
      this.form = { name: '', path: '', sort: 1, isMenu: '0', pid: null }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
